package ld.servlet;

import com.alibaba.fastjson.JSON;
import ld.pojo.PageResult;
import ld.pojo.book;
import ld.service.BookService;
import ld.service.impl.BookServiceImpl;
import ld.vo.BookVo;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * @Author: Liudao
 * @Date: 2022-07-27-15:42
 * @Description:
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {

    private BookService bookService;

    @Override
    public void init(){
        bookService = new BookServiceImpl();
    }

    @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //设置请求参数的编码
        req.setCharacterEncoding("UTF-8");
        //设置响应参数的字符编码类型
        resp.setContentType("text/html;charset=utf8");
        //得到请求参数
        String cmd = req.getParameter("cmd");
        //根据参数调用不同的方法
        if (StringUtils.isNotBlank(cmd)){
            if ("list".equals(cmd))
                list(resp);    //查看所有
            if ("findByPage".equals(cmd))
                findByPage(req,resp);   //分页查看
            if("search".equals(cmd))
                search(req,resp);
            if("add".equals(cmd))
                add(req,resp);
            if("delbk".equals(cmd))
                delbk(req,resp);
        }
    }


    private void list(HttpServletResponse resp) throws IOException {

        //得到所有图书对象
        List<book> AllBooks = bookService.findAll();
        //转换为json对象
        String s = JSON.toJSONString(AllBooks);
        //将字符串输出到网页中
        resp.getWriter().println(s);
        //刷新流
        resp.getWriter().flush();
    }

    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1、得到分页参数
        //1.1得到当前页
        int page = StringUtils.isNotBlank(req.getParameter("page")) ? new Integer(req.getParameter("page")) : 1;
        //1.2得到每页大小
        int pagesize = StringUtils.isNotBlank(req.getParameter("pagesize")) ? new Integer(req.getParameter("pagesize")) : 6;
        //开始分页
        PageResult<book> byPage = bookService.findByPage(page, pagesize);
        //转换为Json对象
        String s = JSON.toJSONString(byPage);
        //输出到网页
        resp.getWriter().println(s);
        //刷新流
        resp.getWriter().flush();
    }

    //条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        //得到分页参数
        //得到当前页
        int page = StringUtils.isNotBlank(req.getParameter("page")) ? Integer.parseInt(req.getParameter("page")) : 1;
        //得到每页大小
        int pagesize = StringUtils.isNotBlank(req.getParameter("pagesize")) ? Integer.parseInt(req.getParameter("pagesize")) : 3;
        //4.2 得到查询关键字
        String booksearch = req.getParameter("booksearch");
        //4.3 处理中文乱码
        booksearch = new String(booksearch.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
        //4.3 将查询学生的字符串转换为学生对象
        BookVo bookVo = JSON.parseObject(booksearch, BookVo.class);
        //4.4 开始查询得到分页结果
        PageResult<book> pr = bookService.search(page,pagesize,bookVo);
        //4.5 将pageInfo转换为json字符串
        String s = JSON.toJSONString(pr);
        //4.6 将字符串输出到网页中
        resp.getWriter().println(s);
        //4.7 刷新流
        resp.getWriter().flush();
    }

    //添加图书
    private boolean add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String s = "";
        try {
            //得到添加的图书信息
//            String Book = req.getParameter("book");
            String Book = new String(req.getParameter("book").getBytes("ISO8859-1"), StandardCharsets.UTF_8);
            //转换为图书对象
            book book = JSON.parseObject(Book,book.class);
            //添加
            return bookService.add(book);
        }catch (IOException e){
            e.printStackTrace();
            return false;
        }finally {
            resp.getWriter().println(s);
            resp.getWriter().flush();
        }
    }

    //删除图书
    private boolean delbk(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            //得到删除的图书id
            String bookidstr = req.getParameter("bookid");
            int bookid = StringUtils.isNotBlank(bookidstr)? new Integer(bookidstr) : 0;
                //删除并返回
                return bookService.delbk(bookid);
        } finally {
            resp.getWriter().println();
            resp.getWriter().flush();
        }
    }
}

